package thread;

import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.FutureTask;

import static java.lang.Thread.sleep;

public class PrintThread {
    public static void main(String[] args) {
        // 第一种实现
//        MyThread thread1 = new MyThread("A");
//        MyThread thread2 = new MyThread("B");
//        MyThread thread3 = new MyThread("C");
//        thread1.start();
//        thread2.start();
//        thread3.start();
        // 第二种实现, 实现Runnable接口
//        Runnable thread = new MyThread1("A");
//        new Thread(thread).start();

        // 第三种实现
        MyThread2 call = new MyThread2("callable实现");

        FutureTask<Integer> futureTask = new FutureTask<>(call);

        new Thread(futureTask).start();
        try {
            System.out.println(futureTask.get());
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        } catch (ExecutionException e) {
            throw new RuntimeException(e);
        }

    }
}
class MyThread extends Thread {
    private String message;

    MyThread(String message) {
        this.message = message;
    }

    @Override
    public void run() {
        for (int i = 1; i <= 10; i++) {
            System.out.print(message);
            try {
                sleep(10);
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
        }
    }

}
// 实现Runnable接口
class MyThread1 implements Runnable {
    private String message;

    MyThread1(String message) {
        this.message = message;
    }
    @Override
    public void run() {
        for (int i = 1; i <= 10; i++) {
            System.out.print(message);
            try {
                sleep(10);
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
        }
    }
}
class MyThread2 implements Callable<Integer> {
    private String message;

    MyThread2(String message) {
        this.message = message;
    }
    @Override
    public Integer call() throws Exception {
        return 10;
    }
}
